home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / rzsz1004.zip / SZ.C < prev    next >
C/C++ Source or Header  |  1991-09-15  |  36KB  |  1,621 lines

  1. #define VERSION "3.16 09-15-91"
  2. #define PUBDIR "/usr/spool/uucppublic"
  3.  
  4. /*% cc -compat -M2 -Ox -K -i -DTXBSIZE=16384  -DNFGVMIN -DREADCHECK sz.c -lx -o sz; size sz
  5.  
  6. <-xtx-*> cc -Osal -DTXBSIZE=32768  -DSV sz.c -lx -o $B/sz; size $B/sz
  7.  
  8.  ****************************************************************************
  9.  *
  10.  * sz.c By Chuck Forsberg,  Omen Technology INC
  11.  *
  12.  ****************************************************************************
  13.  *
  14.  * Typical Unix/Xenix/Clone compiles:
  15.  *
  16.  *    cc -O sz.c -o sz        USG (SYS III/V) Unix
  17.  *    cc -O -DSV sz.c -o sz        Sys V Release 2 with non-blocking input
  18.  *                    Define to allow reverse channel checking
  19.  *    cc -O -DV7  sz.c -o sz        Unix Version 7, 2.8 - 4.3 BSD
  20.  *
  21.  *    cc -O -K -i -DNFGVMIN -DREADCHECK sz.c -lx -o sz    Classic Xenix
  22.  *
  23.  *    ln sz sb            **** All versions ****
  24.  *    ln sz sx            **** All versions ****
  25.  *
  26.  ****************************************************************************
  27.  ****************************************************************************
  28.  *
  29.  *
  30.  * A program for Unix to send files and commands to computers running
  31.  *  Professional-YAM, PowerCom, YAM, IMP, or programs supporting Y/XMODEM.
  32.  *    Copyright 1991 Omen Technology Inc All Rights Reserved
  33.  *
  34.  *  Sz uses buffered I/O to greatly reduce CPU time compared to UMODEM.
  35.  *
  36.  *  USG UNIX (3.0) ioctl conventions courtesy Jeff Martin
  37.  *
  38.  * 
  39.  *    This version implements numerous enhancements including ZMODEM
  40.  *    Run Length Encoding and variable length headers.  These
  41.  *    features were not funded by the original Telenet development
  42.  *    contract.
  43.  * 
  44.  * This software may be freely used for non commercial and
  45.  * educational (didactic only) purposes.  This software may also
  46.  * be freely used to support file transfer operations to or from
  47.  * licensed Omen Technology products.  Any programs which use
  48.  * part or all of this software must be provided in source form
  49.  * with this notice intact except by written permission from Omen
  50.  * Technology Incorporated.
  51.  * 
  52.  * Use of this software for commercial or administrative purposes
  53.  * except when exclusively limited to interfacing Omen Technology
  54.  * products requires a per port license payment of $20.00 US per
  55.  * port (less in quantity).  Use of this code by inclusion,
  56.  * decompilation, reverse engineering or any other means
  57.  * constitutes agreement to these conditions and acceptance of
  58.  * liability to license the materials and payment of reasonable
  59.  * legal costs necessary to enforce this license agreement.
  60.  *
  61.  *
  62.  *        Omen Technology Inc        FAX: 503-621-3745
  63.  *        Post Office Box 4681
  64.  *        Portland OR 97208
  65.  *
  66.  *    This code is made available in the hope it will be useful,
  67.  *    BUT WITHOUT ANY WARRANTY OF ANY KIND OR LIABILITY FOR ANY
  68.  *    DAMAGES OF ANY KIND.
  69.  */
  70.  
  71.  
  72. char *substr(), *getenv();
  73.  
  74. #define LOGFILE "/tmp/szlog"
  75. #include <stdio.h>
  76. #include <signal.h>
  77. #include <setjmp.h>
  78. #include <ctype.h>
  79. #include <errno.h>
  80. extern int errno;
  81. #define STATIC
  82.  
  83. #define PATHLEN 256
  84. #define OK 0
  85. #define FALSE 0
  86. #ifdef TRUE
  87. #undef TRUE
  88. #endif
  89. #define TRUE 1
  90. #define ERROR (-1)
  91. /* Ward Christensen / CP/M parameters - Don't change these! */
  92. #define ENQ 005
  93. #define CAN ('X'&037)
  94. #define XOFF ('s'&037)
  95. #define XON ('q'&037)
  96. #define SOH 1
  97. #define STX 2
  98. #define EOT 4
  99. #define ACK 6
  100. #define NAK 025
  101. #define CPMEOF 032
  102. #define WANTCRC 0103    /* send C not NAK to get crc not checksum */
  103. #define WANTG 0107    /* Send G not NAK to get nonstop batch xmsn */
  104. #define TIMEOUT (-2)
  105. #define RCDO (-3)
  106. #define GCOUNT (-4)
  107. #define RETRYMAX 10
  108.  
  109.  
  110. #define HOWMANY 2
  111. STATIC int Zmodem=0;        /* ZMODEM protocol requested by receiver */
  112. unsigned Baudrate=4800;        /* Default, set by first mode() call */
  113. STATIC unsigned Effbaud = 4800;
  114. STATIC unsigned Txwindow;    /* Control the size of the transmitted window */
  115. STATIC unsigned Txwspac;    /* Spacing between zcrcq requests */
  116. STATIC unsigned Txwcnt;    /* Counter used to space ack requests */
  117. STATIC long Lrxpos;        /* Receiver's last reported offset */
  118. STATIC int errors;
  119. char endmsg[80] = {0};    /* Possible message to display on exit */
  120.  
  121. #include "rbsb.c"    /* most of the system dependent stuff here */
  122.  
  123. #include "crctab.c"
  124.  
  125. STATIC int Filesleft;
  126. STATIC long Totalleft;
  127.  
  128. /*
  129.  * Attention string to be executed by receiver to interrupt streaming data
  130.  *  when an error is detected.  A pause (0336) may be needed before the
  131.  *  ^C (03) or after it.
  132.  */
  133. #ifdef READCHECK
  134. STATIC char Myattn[] = { 0 };
  135. #else
  136. #ifdef USG
  137. STATIC char Myattn[] = { 03, 0336, 0 };
  138. #endif
  139. #endif
  140.  
  141. FILE *in;
  142.  
  143. #ifdef BADSEEK
  144. STATIC int Canseek = 0;    /* 1: Can seek 0: only rewind -1: neither (pipe) */
  145. #ifndef TXBSIZE
  146. #define TXBSIZE 16384        /* Must be power of two, < MAXINT */
  147. #endif
  148. #else
  149. STATIC int Canseek = 1;    /* 1: Can seek 0: only rewind -1: neither (pipe) */
  150. #endif
  151.  
  152. #ifdef TXBSIZE
  153. #define TXBMASK (TXBSIZE-1)
  154. STATIC char Txb[TXBSIZE];        /* Circular buffer for file reads */
  155. STATIC char *txbuf = Txb;        /* Pointer to current file segment */
  156. #else
  157. STATIC char txbuf[1024];
  158. #endif
  159. STATIC long vpos = 0;            /* Number of bytes read from file */
  160.  
  161. STATIC char Lastrx;
  162. STATIC char Crcflg;
  163. STATIC int Modem2=0;        /* XMODEM Protocol - don't send pathnames */
  164. STATIC int Restricted=0;    /* restricted; no /.. or ../ in filenames */
  165. STATIC int Fullname=0;        /* transmit full pathname */
  166. STATIC int Unlinkafter=0;    /* Unlink file after it is sent */
  167. STATIC int Dottoslash=0;    /* Change foo.bar.baz to foo/bar/baz */
  168. STATIC int firstsec;
  169. STATIC int errcnt=0;        /* number of files unreadable */
  170. STATIC int blklen=128;        /* length of transmitted records */
  171. STATIC int Optiong;        /* Let it rip no wait for sector ACK's */
  172. STATIC int Eofseen;        /* EOF seen on input set by zfilbuf */
  173. STATIC int BEofseen;        /* EOF seen on input set by fooseek */
  174. STATIC int Totsecs;        /* total number of sectors this file */
  175. STATIC int Filcnt=0;        /* count of number of files opened */
  176. STATIC unsigned Rxbuflen=16384;    /* Receiver's max buffer length */
  177. STATIC int Tframlen = 0;    /* Override for tx frame length */
  178. STATIC int blkopt=0;        /* Override value for zmodem blklen */
  179. STATIC int Rxflags = 0;
  180. STATIC long bytcnt;
  181. STATIC int Wantfcs32 = TRUE;    /* want to send 32 bit FCS */
  182. STATIC char Lzconv;    /* Local ZMODEM file conversion request */
  183. STATIC char Lzmanag;    /* Local ZMODEM file management request */
  184. STATIC int Lskipnocor;
  185. STATIC char Lztrans;
  186. STATIC int Command;        /* Send a command, then exit. */
  187. STATIC char *Cmdstr;        /* Pointer to the command string */
  188. STATIC int Cmdtries = 11;
  189. STATIC int Cmdack1;        /* Rx ACKs command, then do it */
  190. STATIC int Exitcode;
  191. STATIC int Test;        /* 1= Force receiver to send Attn, etc with qbf. */
  192.             /* 2= Character transparency test */
  193. STATIC char *qbf=
  194.  "The quick brown fox jumped over the lazy dog's back 1234567890\r\n";
  195. STATIC long Lastsync;        /* Last offset to which we got a ZRPOS */
  196. STATIC int Beenhereb4;        /* How many times we've been ZRPOS'd same place */
  197.  
  198. STATIC jmp_buf tohere;        /* For the interrupt on RX timeout */
  199. STATIC jmp_buf intrjmp;    /* For the interrupt on RX CAN */
  200.  
  201. #ifdef XARGSFILE
  202. char *
  203. mystrsave(s)
  204. char *s;
  205. {
  206.     register char *p;
  207.     char *malloc();
  208.  
  209.     if (p = malloc(strlen(s)+1) ) {
  210.         strcpy(p, s); return p;
  211.     }
  212.     fprintf(stderr, "No memory for mystrsave!\n");
  213.     exit(1);
  214. }
  215.  
  216. /* Remove (presumably) terminating CR and/or LF from string */
  217. uncrlf(s)
  218. register char *s;
  219. {
  220.     for ( ; *s; ++s)
  221.         switch (*s) {
  222.         case '\r':
  223.         case '\n':
  224.             *s = 0;  return;
  225.         }
  226. }
  227. #endif
  228.  
  229.  
  230. /* called by signal interrupt or terminate to clean things up */
  231. bibi(n)
  232. {
  233.     canit(); fflush(stdout); mode(0);
  234.     fprintf(stderr, "sz: caught signal %d; exiting\n", n);
  235.     if (n == SIGQUIT)
  236.         abort();
  237.     if (n == 99)
  238.         fprintf(stderr, "mode(2) in rbsb.c not implemented!!\n");
  239.     exit(3);
  240. }
  241. /* Called when ZMODEM gets an interrupt (^X) */
  242. onintr()
  243. {
  244.     signal(SIGINT, SIG_IGN);
  245.     longjmp(intrjmp, -1);
  246. }
  247.  
  248. STATIC int Zctlesc;    /* Encode control characters */
  249. STATIC int Nozmodem = 0;    /* If invoked as "sb" */
  250. STATIC char *Progname = "sz";
  251. STATIC int Zrwindow = 1400;    /* RX window size (controls garbage count) */
  252. #include "zm.c"
  253.  
  254. #include "zmr.c"
  255.  
  256. #ifdef XARGSFILE
  257. #define XARGSMAX 256
  258. char *xargv[XARGSMAX+1];
  259. #endif
  260.  
  261. main(argc, argv)
  262. char *argv[];
  263. {
  264.     register char *cp;
  265.     register npats;
  266.     int dm;
  267.     char **patts;
  268.  
  269.     if ((cp = getenv("ZNULLS")) && *cp)
  270.         Znulls = atoi(cp);
  271.     if ((cp=getenv("SHELL")) && (substr(cp, "rsh") || substr(cp, "rksh")))
  272.         Restricted=TRUE;
  273.     inittty();
  274.     chkinvok(argv[0]);
  275.  
  276.     Rxtimeout = 600;
  277.     npats=0;
  278.     if (argc<2)
  279.         usage();
  280.     while (--argc) {
  281.         cp = *++argv;
  282.         if (*cp++ == '-' && *cp) {
  283.             while ( *cp) {
  284.                 switch(*cp++) {
  285.                 case '\\':
  286.                      *cp = toupper(*cp);  continue;
  287.                 case '+':
  288.                     Lzmanag = ZMAPND; break;
  289. #ifdef CSTOPB
  290.                 case '2':
  291.                     Twostop = TRUE; break;
  292. #endif
  293.                 case 'a':
  294.                     if (Nozmodem || Modem2)
  295.                         usage;
  296.                     Lzconv = ZCNL;  break;
  297.                 case 'b':
  298.                     Lzconv = ZCBIN; break;
  299.                 case 'C':
  300.                     if (--argc < 1) {
  301.                         usage();
  302.                     }
  303.                     Cmdtries = atoi(*++argv);
  304.                     break;
  305.                 case 'c':
  306.                     Lzmanag = ZMCHNG;  break;
  307.                 case 'd':
  308.                     ++Dottoslash;
  309.                     /* **** FALL THROUGH TO **** */
  310.                 case 'f':
  311.                     Fullname=TRUE; break;
  312.                 case 'e':
  313.                     Zctlesc = 1; break;
  314.                 case 'k':
  315.                     blklen=1024; break;
  316.                 case 'L':
  317.                     if (--argc < 1) {
  318.                         usage();
  319.                     }
  320.                     blkopt = atoi(*++argv);
  321.                     if (blkopt<24 || blkopt>1024)
  322.                         usage();
  323.                     break;
  324.                 case 'l':
  325.                     if (--argc < 1) {
  326.                         usage();
  327.                     }
  328.                     Tframlen = atoi(*++argv);
  329.                     if (Tframlen<32 || Tframlen>1024)
  330.                         usage();
  331.                     break;
  332.                 case 'N':
  333.                     Lzmanag = ZMNEWL;  break;
  334.                 case 'n':
  335.                     Lzmanag = ZMNEW;  break;
  336.                 case 'o':
  337.                     Wantfcs32 = FALSE; break;
  338.                 case 'p':
  339.                     Lzmanag = ZMPROT;  break;
  340.                 case 'r':
  341.                     if (Lzconv == ZCRESUM)
  342.                         Lzmanag = (Lzmanag & ZMMASK) | ZMCRC;
  343.                     Lzconv = ZCRESUM; break;
  344.                 case 't':
  345.                     if (--argc < 1) {
  346.                         usage();
  347.                     }
  348.                     Rxtimeout = atoi(*++argv);
  349.                     if (Rxtimeout<10 || Rxtimeout>1000)
  350.                         usage();
  351.                     break;
  352.                 case 'T':
  353.                     if (++Test > 1) {
  354.                         chartest(1); chartest(2);
  355.                         mode(0);  exit(0);
  356.                     }
  357.                     break;
  358.                 case 'u':
  359.                     ++Unlinkafter; break;
  360.                 case 'v':
  361.                     ++Verbose; break;
  362.                 case 'w':
  363.                     if (--argc < 1) {
  364.                         usage();
  365.                     }
  366.                     Txwindow = atoi(*++argv);
  367.                     if (Txwindow < 256)
  368.                         Txwindow = 256;
  369.                     Txwindow = (Txwindow/64) * 64;
  370.                     Txwspac = Txwindow/4;
  371.                     if (blkopt > Txwspac
  372.                      || (!blkopt && Txwspac < 1024))
  373.                         blkopt = Txwspac;
  374.                     break;
  375.                 case 'Y':
  376.                     Lskipnocor = TRUE;
  377.                     /* **** FALLL THROUGH TO **** */
  378.                 case 'y':
  379.                     Lzmanag = ZMCLOB; break;
  380.                 case 'Z':
  381.                 case 'z':
  382.                     Lztrans = ZTRLE;  break;
  383.                 default:
  384.                     usage();
  385.                 }
  386.             }
  387.         }
  388.         else if (Command) {
  389.             if (argc != 1) {
  390.                 usage();
  391.             }
  392.             Cmdstr = *argv;
  393.         }
  394.         else if ( !npats && argc>0) {
  395.             if (argv[0][0]) {
  396.                 npats=argc;
  397.                 patts=argv;
  398.             }
  399.         }
  400.     }
  401.     if (npats < 1 && !Command && !Test) 
  402.         usage();
  403.     if (Verbose) {
  404.         if (freopen(LOGFILE, "a", stderr)==NULL) {
  405.             printf("Can't open log file %s\n",LOGFILE);
  406.             exit(2);
  407.         }
  408.         setbuf(stderr, NULL);
  409.     }
  410.     vfile("%s %s for %s\n", Progname, VERSION, OS);
  411.  
  412. #ifdef XARGSFILE
  413.     vfile("npats=%d *patts=%s", npats, *patts);
  414.     if (npats == 1 && !strcmp(XARGSFILE, *patts)) {
  415.         in = fopen(XARGSFILE, "r");
  416.         if (!in) {
  417.             printf(stderr, "Can't open / control file!\n");
  418.             exit(2);
  419.         }
  420.         for (npats=0,argv=patts=xargv; npats<XARGSMAX; ++npats,++argv) {
  421.             if (fgets(txbuf, 1024, in) <= 0)
  422.                 break;
  423.             uncrlf(txbuf);
  424.             *argv = mystrsave(txbuf);
  425.         }
  426.         fclose(in);
  427.     }
  428. #endif
  429.  
  430.     mode(1);
  431.  
  432.     if (signal(SIGINT, bibi) == SIG_IGN) {
  433.         signal(SIGINT, SIG_IGN); signal(SIGKILL, SIG_IGN);
  434.     } else {
  435.         signal(SIGINT, bibi); signal(SIGKILL, bibi);
  436.     }
  437. #ifdef SIGQUIT
  438.     signal(SIGQUIT, SIG_IGN);
  439. #endif
  440. #ifdef SIGTERM
  441.     signal(SIGTERM, bibi);
  442. #endif
  443.  
  444.     if ( !Modem2) {
  445.         if (!Nozmodem) {
  446.             printf("rz\r");  fflush(stdout);
  447.         }
  448.         countem(npats, patts);
  449.         if (!Nozmodem) {
  450.             stohdr(0L);
  451.             if (Command)
  452.                 Txhdr[ZF0] = ZCOMMAND;
  453.             zshhdr(4, ZRQINIT, Txhdr);
  454.         }
  455.     }
  456.     fflush(stdout);
  457.  
  458.     if (Command) {
  459.         if (getzrxinit()) {
  460.             Exitcode=1; canit();
  461.         }
  462.         else if (zsendcmd(Cmdstr, 1+strlen(Cmdstr))) {
  463.             Exitcode=1; canit();
  464.         }
  465.     } else if (wcsend(npats, patts)==ERROR) {
  466.         Exitcode=1;
  467.         canit();
  468.     }
  469.     if (endmsg[0])
  470.         printf("  %s: %s\r\n", Progname, endmsg);
  471.     printf("%s %s finished.\r\n", Progname, VERSION);
  472.     fflush(stdout);
  473.     mode(0);
  474.     if(errcnt || Exitcode) {
  475.         exit(1);
  476.     }
  477.     exit(0);
  478.     /*NOTREACHED*/
  479. }
  480.  
  481. wcsend(argc, argp)
  482. char *argp[];
  483. {
  484.     register n;
  485.  
  486.     Crcflg=FALSE;
  487.     firstsec=TRUE;
  488.     bytcnt = -1;
  489.     if (Nozmodem) {
  490.         printf("Start your YMODEM receive. ");  fflush(stdout);
  491.     }
  492.     for (n=0; n<argc; ++n) {
  493.         Totsecs = 0;
  494.         if (wcs(argp[n])==ERROR)
  495.             return ERROR;
  496.     }
  497.     Totsecs = 0;
  498.     if (Filcnt==0) {    /* bitch if we couldn't open ANY files */
  499.         if (!Nozmodem && !Modem2) {
  500.             Command = TRUE;
  501.             Cmdstr = "echo \"sz: Can't open any requested files\"";
  502.             if (getnak()) {
  503.                 Exitcode=1; canit();
  504.             }
  505.             if (!Zmodem)
  506.                 canit();
  507.             else if (zsendcmd(Cmdstr, 1+strlen(Cmdstr))) {
  508.                 Exitcode=1; canit();
  509.             }
  510.             Exitcode = 1; return OK;
  511.         }
  512.         canit();
  513.         sprintf(endmsg, "Can't open any requested files");
  514.         return ERROR;
  515.     }
  516.     if (Zmodem)
  517.         saybibi();
  518.     else if ( !Modem2)
  519.         wctxpn("");
  520.     return OK;
  521. }
  522.  
  523. wcs(oname)
  524. char *oname;
  525. {
  526.     register c;
  527.     register char *p, *q;
  528.     struct stat f;
  529.     char name[PATHLEN];
  530.  
  531.     strcpy(name, oname);
  532.  
  533.     if (Restricted) {
  534.         /* restrict pathnames to current tree or uucppublic */
  535.         if ( substr(name, "../")
  536.          || (name[0]== '/' && strncmp(name, PUBDIR, strlen(PUBDIR))) ) {
  537.             canit();  sprintf(endmsg,"Security Violation");
  538.             return ERROR;
  539.         }
  540.     }
  541.  
  542.     in=fopen(oname, ROPMODE);
  543.  
  544.     if (in==NULL) {
  545.         ++errcnt;
  546.         return OK;    /* pass over it, there may be others */
  547.     }
  548.     BEofseen = Eofseen = 0;  vpos = 0;
  549.  
  550.     /* Check for directory or block special files */
  551.     fstat(fileno(in), &f);
  552.     c = f.st_mode & S_IFMT;
  553.     if (c == S_IFDIR || c == S_IFBLK) {
  554.         fclose(in);
  555.         return OK;
  556.     }
  557.  
  558.     ++Filcnt;
  559.     switch (wctxpn(name)) {
  560.     case ERROR:
  561.         return ERROR;
  562.     case ZSKIP:
  563.         return OK;
  564.     }
  565.     if (!Zmodem && wctx(f.st_size)==ERROR)
  566.         return ERROR;
  567.  
  568.     if (Unlinkafter)
  569.         unlink(oname);
  570.  
  571.     return 0;
  572. }
  573.  
  574. /*
  575.  * generate and transmit pathname block consisting of
  576.  *  pathname (null terminated),
  577.  *  file length, mode time and file mode in octal
  578.  *  as provided by the Unix fstat call.
  579.  *  N.B.: modifies the passed name, may extend it!
  580.  */
  581. wctxpn(name)
  582. char *name;
  583. {
  584.     register char *p, *q;
  585.     char name2[PATHLEN];
  586.     struct stat f;
  587.  
  588.     if (Modem2) {
  589.         if (*name && fstat(fileno(in), &f)!= -1) {
  590.             fprintf(stderr, "Sending %s, %ld XMODEM blocks. ",
  591.               name, (127+f.st_size)>>7);
  592.         }
  593.         fprintf(stderr, "Give your local XMODEM receive command now.\r\n");
  594.         fflush(stderr);
  595.         return OK;
  596.     }
  597.     zperr("Awaiting pathname nak for %s", *name?name:"<END>");
  598.     if ( !Zmodem)
  599.         if (getnak())
  600.             return ERROR;
  601.  
  602.     q = (char *) 0;
  603.     if (Dottoslash) {        /* change . to . */
  604.         for (p=name; *p; ++p) {
  605.             if (*p == '/')
  606.                 q = p;
  607.             else if (*p == '.')
  608.                 *(q=p) = '/';
  609.         }
  610.         if (q && strlen(++q) > 8) {    /* If name>8 chars */
  611.             q += 8;            /*   make it .ext */
  612.             strcpy(name2, q);    /* save excess of name */
  613.             *q = '.';
  614.             strcpy(++q, name2);    /* add it back */
  615.         }
  616.     }
  617.  
  618.     for (p=name, q=txbuf ; *p; )
  619.         if ((*q++ = *p++) == '/' && !Fullname)
  620.             q = txbuf;
  621.     *q++ = 0;
  622.     p=q;
  623.     while (q < (txbuf + 1024))
  624.         *q++ = 0;
  625.     if (*name) {
  626.         if (fstat(fileno(in), &f)!= -1)
  627.             sprintf(p, "%lu %lo %o 0 %d %ld", f.st_size, f.st_mtime,
  628.               f.st_mode, Filesleft, Totalleft);
  629.         Totalleft -= f.st_size;
  630.     }
  631.     if (--Filesleft <= 0)
  632.         Filesleft = Totalleft = 0;
  633.     if (Totalleft < 0)
  634.         Totalleft = 0;
  635.  
  636.     /* force 1k blocks if name won't fit in 128 byte block */
  637.     if (txbuf[125])
  638.         blklen=1024;
  639.     else {        /* A little goodie for IMP/KMD */
  640.         txbuf[127] = (f.st_size + 127) >>7;
  641.         txbuf[126] = (f.st_size + 127) >>15;
  642.     }
  643.     if (Zmodem)
  644.         return zsendfile(txbuf, 1+strlen(p)+(p-txbuf));
  645.     if (wcputsec(txbuf, 0, 128)==ERROR)
  646.         return ERROR;
  647.     return OK;
  648. }
  649.  
  650. getnak()
  651. {
  652.     register firstch;
  653.  
  654.     Lastrx = 0;
  655.     for (;;) {
  656.         switch (firstch = readline(800)) {
  657.         case ZPAD:
  658.             if (getzrxinit())
  659.                 return ERROR;
  660.             return FALSE;
  661.         case TIMEOUT:
  662.             sprintf(endmsg, "Timeout waiting for ZRINIT");
  663.             return TRUE;
  664.         case WANTG:
  665. #ifdef MODE2OK
  666.             mode(2);    /* Set cbreak, XON/XOFF, etc. */
  667. #endif
  668.             Optiong = TRUE;
  669.             blklen=1024;
  670.         case WANTCRC:
  671.             Crcflg = TRUE;
  672.         case NAK:
  673.             return FALSE;
  674.         case CAN:
  675.             if ((firstch = readline(20)) == CAN && Lastrx == CAN) {
  676.                 sprintf(endmsg, "Got CAN waiting to send file");
  677.                 return TRUE;
  678.             }
  679.         default:
  680.             break;
  681.         }
  682.         Lastrx = firstch;
  683.     }
  684. }
  685.  
  686.  
  687. wctx(flen)
  688. long flen;
  689. {
  690.     register int thisblklen;
  691.     register int sectnum, attempts, firstch;
  692.     long charssent;
  693.  
  694.     charssent = 0;  firstsec=TRUE;  thisblklen = blklen;
  695.     vfile("wctx:file length=%ld", flen);
  696.  
  697.     while ((firstch=readline(Rxtimeout))!=NAK && firstch != WANTCRC
  698.       && firstch != WANTG && firstch!=TIMEOUT && firstch!=CAN)
  699.         ;
  700.     if (firstch==CAN) {
  701.         zperr("Receiver CANcelled");
  702.         return ERROR;
  703.     }
  704.     if (firstch==WANTCRC)
  705.         Crcflg=TRUE;
  706.     if (firstch==WANTG)
  707.         Crcflg=TRUE;
  708.     sectnum=0;
  709.     for (;;) {
  710.         if (flen <= (charssent + 896L))
  711.             thisblklen = 128;
  712.         if ( !filbuf(txbuf, thisblklen))
  713.             break;
  714.         if (wcputsec(txbuf, ++sectnum, thisblklen)==ERROR)
  715.             return ERROR;
  716.         charssent += thisblklen;
  717.     }
  718.     fclose(in);
  719.     attempts=0;
  720.     do {
  721.         purgeline();
  722.         sendline(EOT);
  723.         flushmo();
  724.         ++attempts;
  725.     }
  726.         while ((firstch=(readline(Rxtimeout)) != ACK) && attempts < RETRYMAX);
  727.     if (attempts == RETRYMAX) {
  728.         zperr("No ACK on EOT");
  729.         return ERROR;
  730.     }
  731.     else
  732.         return OK;
  733. }
  734.  
  735. wcputsec(buf, sectnum, cseclen)
  736. char *buf;
  737. int sectnum;
  738. int cseclen;    /* data length of this sector to send */
  739. {
  740.     register checksum, wcj;
  741.     register char *cp;
  742.     unsigned oldcrc;
  743.     int firstch;
  744.     int attempts;
  745.  
  746.     firstch=0;    /* part of logic to detect CAN CAN */
  747.  
  748.     if (Verbose>2)
  749.         fprintf(stderr, "Sector %3d %2dk\n", Totsecs, Totsecs/8 );
  750.     else if (Verbose>1)
  751.         fprintf(stderr, "\rSector %3d %2dk ", Totsecs, Totsecs/8 );
  752.     for (attempts=0; attempts <= RETRYMAX; attempts++) {
  753.         Lastrx= firstch;
  754.         sendline(cseclen==1024?STX:SOH);
  755.         sendline(sectnum);
  756.         sendline(-sectnum -1);
  757.         oldcrc=checksum=0;
  758.         for (wcj=cseclen,cp=buf; --wcj>=0; ) {
  759.             sendline(*cp);
  760.             oldcrc=updcrc((0377& *cp), oldcrc);
  761.             checksum += *cp++;
  762.         }
  763.         if (Crcflg) {
  764.             oldcrc=updcrc(0,updcrc(0,oldcrc));
  765.             sendline((int)oldcrc>>8);
  766.             sendline((int)oldcrc);
  767.         }
  768.         else
  769.             sendline(checksum);
  770.         flushmo();
  771.  
  772.         if (Optiong) {
  773.             firstsec = FALSE; return OK;
  774.         }
  775.         firstch = readline(Rxtimeout);
  776. gotnak:
  777.         switch (firstch) {
  778.         case CAN:
  779.             if(Lastrx == CAN) {
  780. cancan:
  781.                 zperr("Cancelled");  return ERROR;
  782.             }
  783.             break;
  784.         case TIMEOUT:
  785.             zperr("Timeout on sector ACK"); continue;
  786.         case WANTCRC:
  787.             if (firstsec)
  788.                 Crcflg = TRUE;
  789.         case NAK:
  790.             zperr("NAK on sector"); continue;
  791.         case ACK: 
  792.             firstsec=FALSE;
  793.             Totsecs += (cseclen>>7);
  794.             return OK;
  795.         case ERROR:
  796.             zperr("Got burst for sector ACK"); break;
  797.         default:
  798.             zperr("Got %02x for sector ACK", firstch); break;
  799.         }
  800.         for (;;) {
  801.             Lastrx = firstch;
  802.             if ((firstch = readline(Rxtimeout)) == TIMEOUT)
  803.                 break;
  804.             if (firstch == NAK || firstch == WANTCRC)
  805.                 goto gotnak;
  806.             if (firstch == CAN && Lastrx == CAN)
  807.                 goto cancan;
  808.         }
  809.     }
  810.     zperr("Retry Count Exceeded");
  811.     return ERROR;
  812. }
  813.  
  814. /* fill buf with count chars padding with ^Z for CPM */
  815. filbuf(buf, count)
  816. register char *buf;
  817. {
  818.     register c, m;
  819.  
  820.     m = read(fileno(in), buf, count);
  821.     if (m <= 0)
  822.         return 0;
  823.     while (m < count)
  824.         buf[m++] = 032;
  825.     return count;
  826. }
  827.  
  828. /* Fill buffer with blklen chars */
  829. zfilbuf()
  830. {
  831.     int n;
  832.  
  833. #ifdef TXBSIZE
  834.     vfile("zfilbuf: bytcnt =%lu vpos=%lu blklen=%d", bytcnt, vpos, blklen);
  835.     /* We assume request is within buffer, or just beyond */
  836.     txbuf = Txb + (bytcnt & TXBMASK);
  837.     if (vpos <= bytcnt) {
  838.         n = fread(txbuf, 1, blklen, in);
  839.  
  840.         vpos += n;
  841.         if (n < blklen)
  842.             Eofseen = 1;
  843.         vfile("zfilbuf: n=%d vpos=%lu Eofseen=%d", n, vpos, Eofseen);
  844.         return n;
  845.     }
  846.     if (vpos >= (bytcnt+blklen))
  847.         return blklen;
  848.     /* May be a short block if crash recovery etc. */
  849.     Eofseen = BEofseen;
  850.     return (vpos - bytcnt);
  851. #else
  852.     n = fread(txbuf, 1, blklen, in);
  853.     if (n < blklen)
  854.         Eofseen = 1;
  855.     return n;
  856. #endif
  857. }
  858.  
  859. #ifdef TXBSIZE
  860. /* Replacement for brain damaged fseek function.  Returns 0==success */
  861. fooseek(fptr, pos, whence)
  862. FILE *fptr;
  863. long pos;
  864. {
  865.     long m, n;
  866.  
  867.     vfile("fooseek: pos =%lu vpos=%lu Canseek=%d", pos, vpos, Canseek);
  868.     /* Seek offset < current buffer */
  869.     if (pos < (vpos -TXBSIZE +1024)) {
  870.         BEofseen = 0;
  871.         if (Canseek > 0) {
  872.             vpos = pos & ~TXBMASK;
  873.             if (vpos >= pos)
  874.                 vpos -= TXBSIZE;
  875.             if (fseek(fptr, vpos, 0))
  876.                 return 1;
  877.         }
  878.         else if (Canseek == 0) {
  879.             if (fseek(fptr, vpos = 0L, 0))
  880.                 return 1;
  881.         } else
  882.             return 1;
  883.         while (vpos < pos) {
  884.             n = fread(Txb, 1, TXBSIZE, fptr);
  885.             vpos += n;
  886.             vfile("n=%d vpos=%ld", n, vpos);
  887.             if (n < TXBSIZE) {
  888.                 BEofseen = 1;
  889.                 break;
  890.             }
  891.         }
  892.         vfile("vpos=%ld", vpos);
  893.         return 0;
  894.     }
  895.     /* Seek offset > current buffer (Crash Recovery, etc.) */
  896.     if (pos > vpos) {
  897.         if (Canseek)
  898.             if (fseek(fptr, vpos = (pos & ~TXBMASK), 0))
  899.                 return 1;
  900.         while (vpos <= pos) {
  901.             txbuf = Txb + (vpos & TXBMASK);
  902.             m = TXBSIZE - (vpos & TXBMASK);
  903.             vfile("m=%ld vpos=%ld", m,vpos);
  904.                 n = fread(txbuf, 1, m, fptr);
  905.             vfile("n=%ld vpos=%ld", n,vpos);
  906.             vpos += n;
  907.             vfile("bo=%d m=%ld vpos=%ld", txbuf-Txb,m,vpos);
  908.             if (n < m) {
  909.                 BEofseen = 1;
  910.                 break;
  911.             }
  912.         }
  913.         return 0;
  914.     }
  915.     /* Seek offset is within current buffer */
  916.     vfile("within buffer: vpos=%ld", vpos);
  917.     return 0;
  918. }
  919. #define fseek fooseek
  920. #endif
  921.  
  922.  
  923. /*
  924.  * Log an error
  925.  */
  926. /*VARARGS1*/
  927. zperr(s,p,u)
  928. char *s, *p, *u;
  929. {
  930.     if (Verbose <= 0)
  931.         return;
  932.     fprintf(stderr, "Retry %d: ", errors);
  933.     fprintf(stderr, s, p, u);
  934.     fprintf(stderr, "\n");
  935. }
  936.  
  937. /*
  938.  * substr(string, token) searches for token in string s
  939.  * returns pointer to token within string if found, NULL otherwise
  940.  */
  941. char *
  942. substr(s, t)
  943. register char *s,*t;
  944. {
  945.     register char *ss,*tt;
  946.     /* search for first char of token */
  947.     for (ss=s; *s; s++)
  948.         if (*s == *t)
  949.             /* compare token with substring */
  950.             for (ss=s,tt=t; ;) {
  951.                 if (*tt == 0)
  952.                     return s;
  953.                 if (*ss++ != *tt++)
  954.                     break;
  955.             }
  956.     return NULL;
  957. }
  958.  
  959. char *usinfo[] = {
  960.     "Send Files and Commands with ZMODEM/YMODEM/XMODEM Protocol\n",
  961.     "Usage:    sz [-2+abcdefklLnNuvwyY] [-] file ...",
  962.     "\t    zcommand [-2Cev] COMMAND",
  963.     "\t    zcommandi [-2Cev] COMMAND",
  964.     "\t    sb [-2adfkuv] [-] file ...",
  965.     "\t    sx [-2akuv] [-] file",
  966.     "\nSee sz.doc for option descriptions and licensing information.",
  967.     ""
  968. };
  969.  
  970. usage()
  971. {
  972.     char **pp;
  973.  
  974.     for (pp=usinfo; **pp; ++pp)
  975.         fprintf(stderr, "%s\n", *pp);
  976.     fprintf(stderr, "\n%s %s for %s by Chuck Forsberg, Omen Technology INC\n",
  977.      Progname, VERSION, OS);
  978.     fprintf(stderr, "\t\t\042The High Reliability Software\042\n");
  979.     fprintf(stderr,"\tCopyright 1991 Omen Technology INC All Rights Reserved\n");
  980.     exit(3);
  981. }
  982.  
  983. /*
  984.  * Get the receiver's init parameters
  985.  */
  986. getzrxinit()
  987. {
  988.     register n;
  989.     struct stat f;
  990.  
  991.     for (n=10; --n>=0; ) {
  992.         
  993.         switch (zgethdr(Rxhdr, 1)) {
  994.         case ZCHALLENGE:    /* Echo receiver's challenge numbr */
  995.             stohdr(Rxpos);
  996.             zshhdr(4, ZACK, Txhdr);
  997.             continue;
  998.         case ZCOMMAND:        /* They didn't see out ZRQINIT */
  999.             stohdr(0L);
  1000.             zshhdr(4, ZRQINIT, Txhdr);
  1001.             continue;
  1002.         case ZRINIT:
  1003.             Rxflags = 0377 & Rxhdr[ZF0];
  1004.             Usevhdrs = Rxhdr[ZF1] & CANVHDR;
  1005.             Txfcs32 = (Wantfcs32 && (Rxflags & CANFC32));
  1006.             Zctlesc |= Rxflags & TESCCTL;
  1007.             Rxbuflen = (0377 & Rxhdr[ZP0])+((0377 & Rxhdr[ZP1])<<8);
  1008.             if ( !(Rxflags & CANFDX))
  1009.                 Txwindow = 0;
  1010.             vfile("Rxbuflen=%d Tframlen=%d", Rxbuflen, Tframlen);
  1011.             signal(SIGINT, SIG_IGN);
  1012. #ifdef MODE2OK
  1013.             mode(2);    /* Set cbreak, XON/XOFF, etc. */
  1014. #endif
  1015.  
  1016. #ifndef READCHECK
  1017. #ifndef USG
  1018.             /* Use 1024 byte frames if no sample/interrupt */
  1019.             if (Rxbuflen < 32 || Rxbuflen > 1024) {
  1020.                 Rxbuflen = 1024;
  1021.                 vfile("Rxbuflen=%d", Rxbuflen);
  1022.             }
  1023. #endif
  1024. #endif
  1025.  
  1026.             /* Override to force shorter frame length */
  1027.             if (Rxbuflen && (Rxbuflen>Tframlen) && (Tframlen>=32))
  1028.                 Rxbuflen = Tframlen;
  1029.             if ( !Rxbuflen && (Tframlen>=32) && (Tframlen<=1024))
  1030.                 Rxbuflen = Tframlen;
  1031.             vfile("Rxbuflen=%d", Rxbuflen);
  1032.  
  1033.             /* If using a pipe for testing set lower buf len */
  1034.             fstat(0, &f);
  1035.             if ((f.st_mode & S_IFMT) != S_IFCHR) {
  1036.                 Rxbuflen = 1024;
  1037.             }
  1038.  
  1039.  
  1040.             /*
  1041.              * If input is not a regular file, force ACK's to
  1042.              *  prevent running beyond the buffer limits
  1043.              */
  1044.             if ( !Command) {
  1045.                 fstat(fileno(in), &f);
  1046.                 if ((f.st_mode & S_IFMT) != S_IFREG) {
  1047.                     Canseek = -1;
  1048. #ifdef TXBSIZE
  1049.                     Txwindow = TXBSIZE - 1024;
  1050.                     Txwspac = TXBSIZE/4;
  1051. #else
  1052.                     return ERROR;
  1053. #endif
  1054.                 }
  1055.             }
  1056.  
  1057.             /* Set initial subpacket length */
  1058.             if (blklen < 1024) {    /* Command line override? */
  1059.                 if (Effbaud > 300)
  1060.                     blklen = 256;
  1061.                 if (Effbaud > 1200)
  1062.                     blklen = 512;
  1063.                 if (Effbaud > 2400)
  1064.                     blklen = 1024;
  1065.             }
  1066.             if (Rxbuflen && blklen>Rxbuflen)
  1067.                 blklen = Rxbuflen;
  1068.             if (blkopt && blklen > blkopt)
  1069.                 blklen = blkopt;
  1070.             vfile("Rxbuflen=%d blklen=%d", Rxbuflen, blklen);
  1071.             vfile("Txwindow = %u Txwspac = %d", Txwindow, Txwspac);
  1072.  
  1073.  
  1074.             if (Lztrans == ZTRLE && (Rxflags & CANRLE))
  1075.                 Txfcs32 = 2;
  1076.             else
  1077.                 Lztrans = 0;
  1078.  
  1079.             return (sendzsinit());
  1080.         case ZCAN:
  1081.         case TIMEOUT:
  1082.             return ERROR;
  1083.         case ZRQINIT:
  1084.             if (Rxhdr[ZF0] == ZCOMMAND)
  1085.                 continue;
  1086.         default:
  1087.             zshhdr(4, ZNAK, Txhdr);
  1088.             continue;
  1089.         }
  1090.     }
  1091.     return ERROR;
  1092. }
  1093.  
  1094. /* Send send-init information */
  1095. sendzsinit()
  1096. {
  1097.     register c;
  1098.  
  1099.     if (Myattn[0] == '\0' && (!Zctlesc || (Rxflags & TESCCTL)))
  1100.         return OK;
  1101.     errors = 0;
  1102.     for (;;) {
  1103.         stohdr(0L);
  1104. #ifdef ALTCANOFF
  1105.         Txhdr[ALTCOFF] = ALTCANOFF;
  1106. #endif
  1107.         if (Zctlesc) {
  1108.             Txhdr[ZF0] |= TESCCTL; zshhdr(4, ZSINIT, Txhdr);
  1109.         }
  1110.         else
  1111.             zsbhdr(4, ZSINIT, Txhdr);
  1112.         zsdata(Myattn, ZATTNLEN, ZCRCW);
  1113.         c = zgethdr(Rxhdr, 1);
  1114.         switch (c) {
  1115.         case ZCAN:
  1116.             return ERROR;
  1117.         case ZACK:
  1118.             return OK;
  1119.         default:
  1120.             if (++errors > 19)
  1121.                 return ERROR;
  1122.             continue;
  1123.         }
  1124.     }
  1125. }
  1126.  
  1127. /* Send file name and related info */
  1128. zsendfile(buf, blen)
  1129. char *buf;
  1130. {
  1131.     register c;
  1132.     register UNSL long crc;
  1133.     long lastcrcrq = -1;
  1134.     char *p;
  1135.  
  1136.     for (errors=0; ++errors<11;) {
  1137.         Txhdr[ZF0] = Lzconv;    /* file conversion request */
  1138.         Txhdr[ZF1] = Lzmanag;    /* file management request */
  1139.         if (Lskipnocor)
  1140.             Txhdr[ZF1] |= ZMSKNOLOC;
  1141.         Txhdr[ZF2] = Lztrans;    /* file transport request */
  1142.         Txhdr[ZF3] = 0;
  1143.         zsbhdr(4, ZFILE, Txhdr);
  1144.         zsdata(buf, blen, ZCRCW);
  1145. again:
  1146.         c = zgethdr(Rxhdr, 1);
  1147.         switch (c) {
  1148.         case ZRINIT:
  1149.             while ((c = readline(50)) > 0)
  1150.                 if (c == ZPAD) {
  1151.                     goto again;
  1152.                 }
  1153.             continue;
  1154.         case ZCAN:
  1155.         case TIMEOUT:
  1156.         case ZABORT:
  1157.         case ZFIN:
  1158.             sprintf(endmsg, "Got %s on pathname", frametypes[c+FTOFFSET]);
  1159.             return ERROR;
  1160.         default:
  1161.             sprintf(endmsg, "Got %d frame type on pathname", c);
  1162.             continue;
  1163.         case ERROR:
  1164.         case ZNAK:
  1165.             continue;
  1166.         case ZCRC:
  1167.             if (Rxpos != lastcrcrq) {
  1168.                 lastcrcrq = Rxpos;
  1169.                 crc = 0xFFFFFFFFL;
  1170.                 if (Canseek >= 0) {
  1171.                     fseek(in, 0L, 0);
  1172.                     while (((c = getc(in)) != EOF) && --lastcrcrq)
  1173.                         crc = UPDC32(c, crc);
  1174.                     crc = ~crc;
  1175.                     clearerr(in);    /* Clear possible EOF */
  1176.                     lastcrcrq = Rxpos;
  1177.                 }
  1178.             }
  1179.             stohdr(crc);
  1180.             zsbhdr(4, ZCRC, Txhdr);
  1181.             goto again;
  1182.         case ZFERR:
  1183.         case ZSKIP:
  1184.             sprintf(endmsg, "File skipped by receiver request");
  1185.             fclose(in); return c;
  1186.         case ZRPOS:
  1187.             /*
  1188.              * Suppress zcrcw request otherwise triggered by
  1189.              * lastyunc==bytcnt
  1190.              */
  1191.             if (fseek(in, Rxpos, 0))
  1192.                 return ERROR;
  1193.             Lastsync = (bytcnt = Txpos = Lrxpos = Rxpos) -1;
  1194.             return zsendfdata();
  1195.         }
  1196.     }
  1197.     fclose(in); return ERROR;
  1198. }
  1199.  
  1200. /* Send the data in the file */
  1201. zsendfdata()
  1202. {
  1203.     register c, e, n;
  1204.     register newcnt;
  1205.     register long tcount = 0;
  1206.     int junkcount;        /* Counts garbage chars received by TX */
  1207.     static int tleft = 6;    /* Counter for test mode */
  1208.  
  1209.     junkcount = 0;
  1210.     Beenhereb4 = FALSE;
  1211. somemore:
  1212.     if (setjmp(intrjmp)) {
  1213. waitack:
  1214.         junkcount = 0;
  1215.         c = getinsync(0);
  1216. gotack:
  1217.         switch (c) {
  1218.         default:
  1219.         case ZCAN:
  1220.             fclose(in);
  1221.             return ERROR;
  1222.         case ZSKIP:
  1223.             fclose(in);
  1224.             return c;
  1225.         case ZACK:
  1226.         case ZRPOS:
  1227.             break;
  1228.         case ZRINIT:
  1229.             fclose(in);
  1230.             return OK;
  1231.         }
  1232. #ifdef READCHECK
  1233.         /*
  1234.          * If the reverse channel can be tested for data,
  1235.          *  this logic may be used to detect error packets
  1236.          *  sent by the receiver, in place of setjmp/longjmp
  1237.          *  rdchk(Tty) returns non 0 if a character is available
  1238.          */
  1239.         while (rdchk(Tty)) {
  1240. #ifdef EATSIT
  1241.             switch (checked)
  1242. #else
  1243.             switch (readline(1))
  1244. #endif
  1245.             {
  1246.             case CAN:
  1247.             case ZPAD:
  1248.                 c = getinsync(1);
  1249.                 goto gotack;
  1250.             case XOFF:        /* Wait a while for an XON */
  1251.             case XOFF|0200:
  1252.                 readline(100);
  1253.             }
  1254.         }
  1255. #endif
  1256.     }
  1257.  
  1258.     signal(SIGINT, onintr);
  1259.     newcnt = Rxbuflen;
  1260.     Txwcnt = 0;
  1261.     stohdr(Txpos);
  1262.     zsbhdr(4, ZDATA, Txhdr);
  1263.  
  1264.     /*
  1265.      * Special testing mode.  This should force receiver to Attn,ZRPOS
  1266.      *  many times.  Each time the signal should be caught, causing the
  1267.      *  file to be started over from the beginning.
  1268.      */
  1269.     if (Test) {
  1270.         if ( --tleft)
  1271.             while (tcount < 20000) {
  1272.                 printf(qbf); fflush(stdout);
  1273.                 tcount += strlen(qbf);
  1274. #ifdef READCHECK
  1275.                 while (rdchk(Tty)) {
  1276. #ifdef EATSIT
  1277.                     switch (checked)
  1278. #else
  1279.                     switch (readline(1))
  1280. #endif
  1281.                     {
  1282.                     case CAN:
  1283.                     case ZPAD:
  1284. #ifdef TCFLSH
  1285.                         ioctl(0, TCFLSH, 1);
  1286. #endif
  1287.                         goto waitack;
  1288.                     case XOFF:    /* Wait for XON */
  1289.                     case XOFF|0200:
  1290.                         readline(100);
  1291.                     }
  1292.                 }
  1293. #endif
  1294.             }
  1295.         signal(SIGINT, SIG_IGN); canit();
  1296.         sleep(3); purgeline(); mode(0);
  1297.         printf("\nsz: Tcount = %ld\n", tcount);
  1298.         if (tleft) {
  1299.             printf("ERROR: Interrupts Not Caught\n");
  1300.             exit(1);
  1301.         }
  1302.         exit(0);
  1303.     }
  1304.  
  1305.     do {
  1306.         n = zfilbuf();
  1307.         if (Eofseen)
  1308.             e = ZCRCE;
  1309.         else if (junkcount > 3)
  1310.             e = ZCRCW;
  1311.         else if (bytcnt == Lastsync)
  1312.             e = ZCRCW;
  1313.         else if (Rxbuflen && (newcnt -= n) <= 0)
  1314.             e = ZCRCW;
  1315.         else if (Txwindow && (Txwcnt += n) >= Txwspac) {
  1316.             Txwcnt = 0;  e = ZCRCQ;
  1317.         } else
  1318.             e = ZCRCG;
  1319.         if (Verbose>1)
  1320.             fprintf(stderr, "\r%7ld ZMODEM%s    ",
  1321.               Txpos, Crc32t?" CRC-32":"");
  1322.         zsdata(txbuf, n, e);
  1323.         bytcnt = Txpos += n;
  1324.         if (e == ZCRCW)
  1325.             goto waitack;
  1326. #ifdef READCHECK
  1327.         /*
  1328.          * If the reverse channel can be tested for data,
  1329.          *  this logic may be used to detect error packets
  1330.          *  sent by the receiver, in place of setjmp/longjmp
  1331.          *  rdchk(Tty) returns non 0 if a character is available
  1332.          */
  1333.         fflush(stdout);
  1334.         while (rdchk(Tty)) {
  1335. #ifdef EATSIT
  1336.             switch (checked)
  1337. #else
  1338.             switch (readline(1))
  1339. #endif
  1340.             {
  1341.             case CAN:
  1342.             case ZPAD:
  1343.                 c = getinsync(1);
  1344.                 if (c == ZACK)
  1345.                     break;
  1346. #ifdef TCFLSH
  1347.                 ioctl(0, TCFLSH, 1);
  1348. #endif
  1349.                 /* zcrce - dinna wanna starta ping-pong game */
  1350.                 zsdata(txbuf, 0, ZCRCE);
  1351.                 goto gotack;
  1352.             case XOFF:        /* Wait a while for an XON */
  1353.             case XOFF|0200:
  1354.                 readline(100);
  1355.             default:
  1356.                 ++junkcount;
  1357.             }
  1358.         }
  1359. #endif    /* READCHECK */
  1360.         if (Txwindow) {
  1361.             while ((tcount = (Txpos - Lrxpos)) >= Txwindow) {
  1362.                 vfile("%ld window >= %u", tcount, Txwindow);
  1363.                 if (e != ZCRCQ)
  1364.                     zsdata(txbuf, 0, e = ZCRCQ);
  1365.                 c = getinsync(1);
  1366.                 if (c != ZACK) {
  1367. #ifdef TCFLSH
  1368.                     ioctl(0, TCFLSH, 1);
  1369. #endif
  1370.                     zsdata(txbuf, 0, ZCRCE);
  1371.                     goto gotack;
  1372.                 }
  1373.             }
  1374.             vfile("window = %ld", tcount);
  1375.         }
  1376.     } while (!Eofseen);
  1377.     signal(SIGINT, SIG_IGN);
  1378.  
  1379.     for (;;) {
  1380.         stohdr(Txpos);
  1381.         zsbhdr(4, ZEOF, Txhdr);
  1382.         switch (getinsync(0)) {
  1383.         case ZACK:
  1384.             continue;
  1385.         case ZRPOS:
  1386.             goto somemore;
  1387.         case ZRINIT:
  1388.             fclose(in);
  1389.             return OK;
  1390.         case ZSKIP:
  1391.             fclose(in);
  1392.             sprintf(endmsg, "File skipped by receiver request");
  1393.             return c;
  1394.         default:
  1395.             sprintf(endmsg, "Got %d trying to send end of file", c);
  1396.             fclose(in);
  1397.             return ERROR;
  1398.         }
  1399.     }
  1400. }
  1401.  
  1402. /*
  1403.  * Respond to receiver's complaint, get back in sync with receiver
  1404.  */
  1405. getinsync(flag)
  1406. {
  1407.     register c;
  1408.  
  1409.     for (;;) {
  1410.         if (Test) {
  1411.             printf("\r\n\n\n***** Signal Caught *****\r\n");
  1412.             Rxpos = 0; c = ZRPOS;
  1413.         } else
  1414.             c = zgethdr(Rxhdr, 0);
  1415.         switch (c) {
  1416.         case ZCAN:
  1417.         case ZABORT:
  1418.         case ZFIN:
  1419.         case TIMEOUT:
  1420.             sprintf(endmsg, "Got %s sending data", frametypes[c+FTOFFSET]);
  1421.             return ERROR;
  1422.         case ZRPOS:
  1423.             /* ************************************* */
  1424.             /*  If sending to a buffered modem, you  */
  1425.             /*   might send a break at this point to */
  1426.             /*   dump the modem's buffer.         */
  1427.             clearerr(in);    /* In case file EOF seen */
  1428.             if (fseek(in, Rxpos, 0))
  1429.                 return ERROR;
  1430.             Eofseen = 0;
  1431.             bytcnt = Lrxpos = Txpos = Rxpos;
  1432.             if (Lastsync == Rxpos) {
  1433.                 if (++Beenhereb4 > 12) {
  1434.                     sprintf(endmsg, "Can't send block");
  1435.                     return ERROR;
  1436.                 }
  1437.                 if (Beenhereb4 > 4)
  1438.                     if (blklen > 32)
  1439.                         blklen /= 2;
  1440.             }
  1441.             Lastsync = Rxpos;
  1442.             return c;
  1443.         case ZACK:
  1444.             Lrxpos = Rxpos;
  1445.             if (flag || Txpos == Rxpos)
  1446.                 return ZACK;
  1447.             continue;
  1448.         case ZRINIT:
  1449.             return c;
  1450.         case ZSKIP:
  1451.             sprintf(endmsg, "File skipped by receiver request");
  1452.             return c;
  1453.         case ERROR:
  1454.         default:
  1455.             zsbhdr(4, ZNAK, Txhdr);
  1456.             continue;
  1457.         }
  1458.     }
  1459. }
  1460.  
  1461.  
  1462. /* Say "bibi" to the receiver, try to do it cleanly */
  1463. saybibi()
  1464. {
  1465.     for (;;) {
  1466.         stohdr(0L);        /* CAF Was zsbhdr - minor change */
  1467.         zshhdr(4, ZFIN, Txhdr);    /*  to make debugging easier */
  1468.         switch (zgethdr(Rxhdr, 0)) {
  1469.         case ZFIN:
  1470.             sendline('O'); sendline('O'); flushmo();
  1471.         case ZCAN:
  1472.         case TIMEOUT:
  1473.             return;
  1474.         }
  1475.     }
  1476. }
  1477.  
  1478. /* Send command and related info */
  1479. zsendcmd(buf, blen)
  1480. char *buf;
  1481. {
  1482.     register c;
  1483.     long cmdnum;
  1484.  
  1485.     cmdnum = getpid();
  1486.     errors = 0;
  1487.     for (;;) {
  1488.         stohdr(cmdnum);
  1489.         Txhdr[ZF0] = Cmdack1;
  1490.         zsbhdr(4, ZCOMMAND, Txhdr);
  1491.         zsdata(buf, blen, ZCRCW);
  1492. listen:
  1493.         Rxtimeout = 100;        /* Ten second wait for resp. */
  1494.         Usevhdrs = 0;        /* Allow rx to send fixed len headers */
  1495.         c = zgethdr(Rxhdr, 1);
  1496.  
  1497.         switch (c) {
  1498.         case ZRINIT:
  1499.             goto listen;    /* CAF 8-21-87 */
  1500.         case ERROR:
  1501.         case GCOUNT:
  1502.         case TIMEOUT:
  1503.             if (++errors > Cmdtries)
  1504.                 return ERROR;
  1505.             continue;
  1506.         case ZCAN:
  1507.         case ZABORT:
  1508.         case ZFIN:
  1509.         case ZSKIP:
  1510.         case ZRPOS:
  1511.             return ERROR;
  1512.         default:
  1513.             if (++errors > 20)
  1514.                 return ERROR;
  1515.             continue;
  1516.         case ZCOMPL:
  1517.             Exitcode = Rxpos;
  1518.             saybibi();
  1519.             return OK;
  1520.         case ZRQINIT:
  1521.             vfile("******** RZ *******");
  1522.             system("rz");
  1523.             vfile("******** SZ *******");
  1524.             goto listen;
  1525.         }
  1526.     }
  1527. }
  1528.  
  1529. /*
  1530.  * If called as sb use YMODEM protocol
  1531.  */
  1532. chkinvok(s)
  1533. char *s;
  1534. {
  1535.     register char *p;
  1536.  
  1537.     p = s;
  1538.     while (*p == '-')
  1539.         s = ++p;
  1540.     while (*p)
  1541.         if (*p++ == '/')
  1542.             s = p;
  1543.     if (*s == 'v') {
  1544.         Verbose=1; ++s;
  1545.     }
  1546.     Progname = s;
  1547.     if (s[0]=='z' && s[1] == 'c') {
  1548.         Command = TRUE;
  1549.         if (s[8] == 'i')
  1550.             Cmdack1 = ZCACK1;
  1551.     }
  1552.     if (s[0]=='s' && s[1]=='b') {
  1553.         Nozmodem = TRUE; blklen=1024;
  1554.     }
  1555.     if (s[0]=='s' && s[1]=='x') {
  1556.         Modem2 = TRUE;
  1557.     }
  1558. }
  1559.  
  1560. countem(argc, argv)
  1561. register char **argv;
  1562. {
  1563.     register c;
  1564.     struct stat f;
  1565.  
  1566.     for (Totalleft = 0, Filesleft = 0; --argc >=0; ++argv) {
  1567.         f.st_size = -1;
  1568.         if (Verbose>2) {
  1569.             fprintf(stderr, "\nCountem: %03d %s ", argc, *argv);
  1570.             fflush(stderr);
  1571.         }
  1572.         if (access(*argv, 04) >= 0 && stat(*argv, &f) >= 0) {
  1573.             c = f.st_mode & S_IFMT;
  1574.             if (c != S_IFDIR && c != S_IFBLK) {
  1575.                 ++Filesleft;  Totalleft += f.st_size;
  1576.             }
  1577.         }
  1578.         if (Verbose>2)
  1579.             fprintf(stderr, " %ld", f.st_size);
  1580.     }
  1581.     if (Verbose>2)
  1582.         fprintf(stderr, "\ncountem: Total %d %ld\n",
  1583.           Filesleft, Totalleft);
  1584. }
  1585.  
  1586. chartest(m)
  1587. {
  1588.     register n;
  1589.  
  1590.     mode(m);
  1591.     printf("\r\n\nCharacter Transparency Test Mode %d\r\n", m);
  1592.     printf("If Pro-YAM/ZCOMM is not displaying ^M hit ALT-V NOW.\r\n");
  1593.     printf("Hit Enter.\021");  fflush(stdout);
  1594.     readline(500);
  1595.  
  1596.     for (n = 0; n < 256; ++n) {
  1597.         if (!(n%8))
  1598.             printf("\r\n");
  1599.         printf("%02x ", n);  fflush(stdout);
  1600.         sendline(n);    flushmo();
  1601.         printf("  ");  fflush(stdout);
  1602.         if (n == 127) {
  1603.             printf("Hit Enter.\021");  fflush(stdout);
  1604.             readline(500);
  1605.             printf("\r\n");  fflush(stdout);
  1606.         }
  1607.     }
  1608.     printf("\021\r\nEnter Characters, echo is in hex.\r\n");
  1609.     printf("Hit SPACE or pause 40 seconds for exit.\r\n");
  1610.  
  1611.     while (n != TIMEOUT && n != ' ') {
  1612.         n = readline(400);
  1613.         printf("%02x\r\n", n);
  1614.         fflush(stdout);
  1615.     }
  1616.     printf("\r\nMode %d character transparency test ends.\r\n", m);
  1617.     fflush(stdout);
  1618. }
  1619.  
  1620. /* End of sz.c */
  1621.